package com.example.smartretaildemo.module.classification.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.smartretaildemo.R;
import com.example.smartretaildemo.databinding.ItemRvClachooseBinding;
import com.example.smartretaildemo.module.classification.model.CalType;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName: ChooseRViewAdapter$
 * @Description: 分类选择RecycleView适配器
 * @Author: letquit
 * @CreateDate: 19/9/2021$
 * @Version: 1.0
 */
public class ChooseRViewAdapter extends RecyclerView.Adapter<ChooseRViewAdapter.ViewHolder>  {
    private List<CalType> list;
    private Context context;
//    记录所选CheckBox
    private static Map<Integer, Boolean> checkStatus = new HashMap<>();
    private Map<Integer, Boolean> map = new HashMap<>();

    public ChooseRViewAdapter(List<CalType> list, Context context) {
        this.list = list;
        this.context = context;
    }

//    全选
    public void selectAll() {
        initCheck(true);
        notifyDataSetChanged();
    }

//    全不选
    public void unSelectAll() {
        initCheck(false);
        notifyDataSetChanged();
    }

//    修改所选
    public void initCheck(boolean flag) {
        for (int i = 0; i < list.size(); i++) {
            checkStatus.put(i, flag);
        }
    }

//    获取所选CheckBox状态
    public static Map<Integer, Boolean> getCheck() {
        return checkStatus;
    }

//    将CheckBox状态插入Map
    public static void setCheck(int position, boolean isChecked) {
        checkStatus.put(position, isChecked);;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_clachoose, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ViewHolder v = (ViewHolder)holder;
//        Log.i("position", String.valueOf(position));
//        禁止holder的复用 直接杜绝了勾选混淆的可能性
//        v.setIsRecyclable(false);
        CalType type = list.get(position);
        String commodityTypeName = type.getCommodityTypeName();
        v.tvRvClaChoName.setText(commodityTypeName);
        SharedPreferences sp = context.getSharedPreferences("calsList01", Context.MODE_PRIVATE);
        Set<String> spSet = new HashSet<>();
        Set<String> mSet = new HashSet<>();
        mSet.add("1");
//        默认分类为1
        spSet = sp.getStringSet("cals", mSet);
        if (spSet.size() != 0) {
            for (String rood : spSet) {
                int iRood = Integer.parseInt(rood) - 1;
//                根据所选更改样式
                if (iRood == position) {
                    v.rvCheckBox.setChecked(true);
                    v.tvRvClaTitle.setTextColor(Color.parseColor("#FF0000"));
                }
            }
        }
//        监听CheckBox状态
        v.rvCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setCheck(position, v.rvCheckBox.isChecked());
                if (isChecked) {
                    map.put(position, true);
                    checkStatus.put(position, isChecked);
                    v.tvRvClaTitle.setTextColor(Color.parseColor("#FF0000"));
                } else {
                    map.remove(position);
                    v.tvRvClaTitle.setTextColor(Color.parseColor("#000000"));
                }
            }
        });
        if (map != null && map.containsKey(position)) {
            v.rvCheckBox.setChecked(true);
        } else {
            v.rvCheckBox.setChecked(false);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ItemRvClachooseBinding itemBinding;
        CheckBox rvCheckBox;
        TextView tvRvClaChoName, tvRvClaTitle;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            itemBinding = ItemRvClachooseBinding.bind(itemView);
            rvCheckBox = itemBinding.rvCheckBox;
            tvRvClaChoName = itemBinding.tvRvClaChoName;
            tvRvClaTitle = itemBinding.tvRvClaTitle;
        }
    }
}
